Adding some more judges, here and there.
[and.git] / SPOJ / PT07X - 1435. Vertex Cover / pt07x.cpp
blob37eef0145de8c62c1dc839589931c24ec36a327d
1 // Andrés Mejía
2 using namespace std;
3 #include <algorithm>
4 #include <iostream>
5 #include <iterator>
6 #include <numeric>
7 #include <sstream>
8 #include <fstream>
9 #include <cassert>
10 #include <climits>
11 #include <cstdlib>
12 #include <cstring>
13 #include <string>
14 #include <cstdio>
15 #include <vector>
16 #include <cmath>
17 #include <queue>
18 #include <deque>
19 #include <stack>
20 #include <list>
21 #include <map>
22 #include <set>
24 #define foreach(x, v) for (typeof (v).begin() x=(v).begin(); x !=(v).end(); ++x)
25 #define For(i, a, b) for (int i=(a); i<(b); ++i)
26 #define D(x) cout << #x " is " << x << endl
28 const double EPS = 1e-9;
29 int cmp(double x, double y = 0, double tol = EPS) {
30 return (x <= y + tol) ? (x + tol < y) ? -1 : 0 : 1;
33 const int MAXN = 100001;
34 vector<int> g[MAXN];
35 int memo[MAXN][2];
37 int f(int node, bool edge, int parent = -1) {
38 assert(g[node].size() > 0);
39 if (g[node].size() == 1 and g[node][0] == parent) { // Base case
40 return edge ? 0 : 1;
43 if (memo[node][edge] > -1) return memo[node][edge];
45 // take this node
46 int ans = 1;
47 for (int k = 0; k < g[node].size(); ++k) {
48 int v = g[node][k];
49 if (v == parent) continue;
50 ans += f(v, true, node);
53 if (edge) {
54 // do not take this node
55 int option = 0;
56 for (int k = 0; k < g[node].size(); ++k) {
57 int v = g[node][k];
58 if (v == parent) continue;
59 option += f(v, false, node);
61 ans = min(ans, option);
64 return memo[node][edge] = ans;
67 int main(){
68 int n; scanf("%d", &n);
69 for (int i = 0; i < n; ++i) {
70 g[i].clear();
71 memo[i][0] = memo[i][1] = -1;
73 for (int i = 0; i < n - 1; ++i) {
74 int u, v; scanf("%d %d", &u, &v);
75 u--; v--;
76 g[u].push_back(v);
77 g[v].push_back(u);
79 if (n == 1) {
80 printf("0\n");
81 } else {
82 printf("%d\n", min(f(0, false), f(0, true)));
84 return 0;